package com.jxau.mall.cart.controller;

import com.jxau.mall.cart.entity.Cart;
import com.jxau.mall.cart.service.ICartService;
import com.jxau.mall.common.util.ResponseVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @program: shop
 * @description: 购物车控制器类
 * @author: guojiani
 * @create: 2025-04-26 16:20
 **/
@RestController
@RequestMapping(value = "/cart")
public class CartController {
    @Autowired
    private ICartService cartService;

    /**
     * 添加购物车
     * id: skuid
     */
    @PostMapping("/save")
    public ResponseVO add(@RequestBody Cart cart){
        // 购物车编号:由用户编号和商品编号组成
        String id = cart.getUserId() + cart.getSkuId();
        cart.set_id(id);
        // 创建时间
        cart.setCreateTime(LocalDateTime.now());
        //加入购物车
        cartService.add(cart);
        return ResponseVO.success();
    }
    /**
     * 购物车列表
     * @return
     */
    @GetMapping(value = "/list/{userId}")
    public ResponseVO<List<Cart>> list(@PathVariable String userId){
        List<Cart> carts = cartService.list(userId);
        return ResponseVO.success(carts);
    }
    @DeleteMapping(value = "/{id}")
    public ResponseVO delete(@PathVariable String id){
        cartService.removeById(id);
        return ResponseVO.success();
    }
}
